package org.fireflow2.designer.eclipse.commands.workflownode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.gef.commands.Command;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.TransitionWrapper;

public class DeleteWorkflowNodeCommand extends Command {
	NodeWrapper nodeWrapper = null;

	Wrapper parentWrapper = null;
	
	Map<String,ModelWrapper> transitionSourceMap = new HashMap<String,ModelWrapper>();
	Map<String,ModelWrapper> transitionTargetMap = new HashMap<String,ModelWrapper>();
	List<TransitionWrapper> leavingTransitions = null;
	List<TransitionWrapper> enteringTransitions = null;
	
	public NodeWrapper getNodeWrapper() {
		return nodeWrapper;
	}

	public void setNodeWrapper(NodeWrapper nodeWrapper) {
		this.nodeWrapper = nodeWrapper;

	}
	
	
	public void setParentWrapper(Wrapper parent){
		this.parentWrapper = parent;
	}
	
	public void execute() {
		leavingTransitions = new ArrayList<TransitionWrapper>(nodeWrapper.getLeavingTransitions());
		enteringTransitions = new ArrayList<TransitionWrapper>(nodeWrapper.getEnteringTransitions());

		
		for (int i=0;i<leavingTransitions.size();i++){
			TransitionWrapper transitionWrapper = (TransitionWrapper)leavingTransitions.get(i);
			transitionSourceMap.put(transitionWrapper.getSn(), transitionWrapper.getSource());
			transitionTargetMap.put(transitionWrapper.getSn(), transitionWrapper.getTarget());
		}
		
		for (int i=0;i<enteringTransitions.size();i++){
			TransitionWrapper transitionWrapper = (TransitionWrapper)enteringTransitions.get(i);
			transitionSourceMap.put(transitionWrapper.getSn(), transitionWrapper.getSource());
			transitionTargetMap.put(transitionWrapper.getSn(), transitionWrapper.getTarget());
		}

		
		//执行delete操作
		for (int i=0;i<leavingTransitions.size();i++){
			deleteTransition((TransitionWrapper)leavingTransitions.get(i));
		}
		
		for (int i=0;i<enteringTransitions.size();i++){
			deleteTransition((TransitionWrapper)enteringTransitions.get(i));
		}
		
		
		parentWrapper.deleteChild(nodeWrapper);
	}
	
	private void deleteTransition(TransitionWrapper transWrapper){
		NodeWrapper source = (NodeWrapper)this.transitionSourceMap.get(transWrapper.getSn());
		NodeWrapper target = (NodeWrapper)this.transitionTargetMap.get(transWrapper.getSn());
		source.removeLeavingTransition(transWrapper);
		target.removeEnteringTransition(transWrapper);
		parentWrapper.deleteChild(transWrapper);
		transWrapper.setSource(null);
		transWrapper.setTarget(null);		
	}
	
	private void addTransition(TransitionWrapper transWrapper){
		NodeWrapper source = (NodeWrapper)this.transitionSourceMap.get(transWrapper.getSn());
		NodeWrapper target = (NodeWrapper)this.transitionTargetMap.get(transWrapper.getSn());
		transWrapper.setSource(source);
		transWrapper.setTarget(target);
		parentWrapper.addChild(transWrapper);
		
		source.addLeavingTransition(transWrapper);
		target.addEnteringTransition(transWrapper);
	}
	

	
	public void redo(){
		//执行delete操作
		for (int i=0;i<leavingTransitions.size();i++){
			deleteTransition((TransitionWrapper)leavingTransitions.get(i));
		}
		
		for (int i=0;i<enteringTransitions.size();i++){
			deleteTransition((TransitionWrapper)enteringTransitions.get(i));
		}

		
		parentWrapper.deleteChild(nodeWrapper);
	}
	
	public void undo(){
		parentWrapper.addChild(nodeWrapper);
		for (int i=0;i<leavingTransitions.size();i++){
			addTransition((TransitionWrapper)leavingTransitions.get(i));
		}
		
		for (int i=0;i<enteringTransitions.size();i++){
			addTransition((TransitionWrapper)enteringTransitions.get(i));
		}
		
	}
}
